#include "stdint.h"
#include "stdio.h"
#include "sdkconfig.h"
#include "cpuusage.h"
#include "parameters.h"

#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include "freertos/semphr.h"
#include "freertos/timers.h"

#include "DevicesKey.h"
#include "DevicesLed.h"
#include "DevicesQueue.h"
#include "DevicesTime.h"
#include "DevicesADC.h"

#include "taskKey.h"
#include "taskSoftTimer.h"


TimerHandle_t g_xTimersLedHandle = NULL;    /* Led定时刷新 */
TimerHandle_t g_xTimersKeyHandle = NULL;    /* Key定时读取 */
TimerHandle_t g_xTimersPrintHandle = NULL;  /* Print定时打印 */


/*
*   功能说明: 定时器led回调函数
*   形    参: 无
*   返 回 值: 无
*/
static void vSoftTimerLedCallback(TimerHandle_t pxTimer)
{
    static uint8_t st_networkState = -1;
    ParametersType *ptypeParameters = ptypeParametersInfoGet();

    (void)pxTimer;

    if(st_networkState != ptypeParameters->networkState)
    {
        st_networkState = ptypeParameters->networkState;

        if((st_networkState & BIT0) == 0)
            vLedFlashFast(LED_CHANNEL_SYSTEM);
        else if((st_networkState & BIT1) == 0)
            vLedFlashSlow(LED_CHANNEL_SYSTEM);
        else if((st_networkState & BIT2) == 0)
            vLedBreathe(LED_CHANNEL_SYSTEM);
        else
            vLedEnable(LED_CHANNEL_SYSTEM);
    }

    vLedMachine();
}

/*
*   功能说明: 定时器key回调函数
*   形    参: 无
*   返 回 值: 无
*/
static void vSoftTimerKeyCallback(TimerHandle_t pxTimer)
{
    (void)pxTimer;

    /* 有触发了新按键状态 */
    if(enumKeyMachine(&g_typeKey) != keyNormal)
    {
        /* 把有效的按键数据存入FIFO */
        enumQueuePushDatas(&g_TypeQueueKeyInput, &g_typeKey, sizeof(g_typeKey));

        /* 给出信号量，以启动任务执行解析 */
        xTaskNotifyGive(g_TaskKeyHand);
    }
}

/*
*   功能说明: 定时器Print回调函数
*   形    参: 无
*   返 回 值: 无
*/
static void vSoftTimerPrintCallback(TimerHandle_t pxTimer)
{
    static int st_iHeapFreeSize = 0;
    ParametersType *ptypeParameters = ptypeParametersInfoGet();
    int iValue;

    (void)pxTimer;

    if(st_iHeapFreeSize != (iValue = esp_get_free_heap_size()))
    {
        st_iHeapFreeSize = iValue;
        printf("Time: %s - CPU: %.2f%%\r\n", pcTimeToStrings(lTimestampGet() / 1000000LL), fCPUUsageGet() * 100.0f);
        printf("Memory free: %d byte, mini: %d byte, net: 0x%02X\r\n", st_iHeapFreeSize, (int)esp_get_minimum_free_heap_size(), ptypeParameters->networkState);
    }
}


/*
*   函 数 名: cSoftTimerInit
*   功能说明: 操作系统软件定时器初始化
*   形    参: 无
*   返 回 值: 错误值
*/
int8_t cSoftTimerInit(void)
{
    /*创建定时器*/
    g_xTimersLedHandle      = xTimerCreate("led",       20 / portTICK_PERIOD_MS,    pdTRUE, (void*)NULL, vSoftTimerLedCallback);
    xTimerStart(g_xTimersLedHandle, 200);

    g_xTimersKeyHandle      = xTimerCreate("key",       20 / portTICK_PERIOD_MS,    pdTRUE, (void*)NULL, vSoftTimerKeyCallback);
    xTimerStart(g_xTimersKeyHandle, 200);

    g_xTimersPrintHandle    = xTimerCreate("print",     1000 / portTICK_PERIOD_MS,  pdTRUE, (void*)NULL, vSoftTimerPrintCallback);
    xTimerStart(g_xTimersPrintHandle, 0);

    return 0;
}
